package com.yd.mvc.pc.MessageGroupController;

import com.yd.app.service.pc.messageGroupService.MessageGroupService;
import com.yd.app.service.sys.UserService;
import com.yd.app.util.BusinessException;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.MessageGroupSearchParam;
import com.yd.domain.sys.DicEnum;
import com.yd.domain.sys.MessageGroup;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2018/6/5.
 */
@RestController
public class MessageGroupController extends BaseController {
    @Autowired
    MessageGroupService messageGroupService;
    @Autowired
    UserService userService;

    /**
     * 添加消息群发
     * @param messageGroup
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/messageGroup/add", method = RequestMethod.POST)
    public void addAgent(@RequestBody MessageGroup messageGroup) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.MESSAGEGROUP_MANAGER);//验证权限
        }
        String id = UUID.randomUUID().toString().replace("-", "");
        messageGroup.setId(id);
        messageGroup.setInputUser(user.getUserId());
        messageGroup.setInputUserName(user.getRealName());
        messageGroup.setInputDate(new Date());
        messageGroupService.addMessageGroup(messageGroup);
    }

    /**
     * 消息群发列表
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/api/messageGroup/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel userList(@RequestBody MessageGroupSearchParam param, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize)throws Exception {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        PageModel pm=null;
        if(user!=null){
            pm=messageGroupService.messageGroupList(param,pageIndex,pageSize,user.getUserId());
        }else{
            throw new BusinessException(401, "未登录");
        }
        return pm;
    }

    /**
     * 通过id删除消息群发
     * @param messageGroupId
     * @throws Exception
     */
    @RequestMapping(value = "/api/messageGroup/delete/{messageGroupId}", method = RequestMethod.DELETE)
    public void userDelete(@PathVariable("messageGroupId")String messageGroupId)throws Exception {
        messageGroupService.deleteMessageGroupById(messageGroupId);
    }

    /**
     * 修改消息群发
     * @param messageGroup
     * @throws Exception
     */
    @RequestMapping(value = "/api/messageGroup/update", method = RequestMethod.POST)
    public void userUpdate(@RequestBody MessageGroup messageGroup)throws Exception {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        messageGroup.setUpdateDate(new Date());
        messageGroup.setUpdateUser(user.getUserId());
        if(messageGroup!=null){
            messageGroupService.updateMessageGroup(messageGroup);
        }
    }

    //获取用户角色
    @RequestMapping(value = "/api/messageGroup/getMessageGroupUser/{messageGroupId}", method = RequestMethod.GET)
    public List<MessageGroup> getUserRoleList(@PathVariable("messageGroupId")String messageGroupId){
        List<MessageGroup> list= messageGroupService.getMessageGroupUserList(messageGroupId);
        return list;
    }
}
